package com.netCross.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;

import java.util.List;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

public class UpdateActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        initPython();
        Intent intent = getIntent();

        List<PyObject> jsonList = callPythonCode(intent.getStringExtra("demand_order_number"));
        EditText editView1 = findViewById(R.id.demand_order_numberEditView);
        EditText editText2 = findViewById(R.id.waybill_idEditView);
        EditText editText3 = findViewById(R.id.type_of_transportEditView);
        EditText editText4 = findViewById(R.id.mode_of_transportEditView);
        EditText editText5 = findViewById(R.id.dangerous_goods_labeEditView);

        for (int i = 0; i < jsonList.size(); i++) {

            //不要怀疑,当前for循环次数为1,为以后勾选多个同时修改做准备
            String strTypeJson = String.valueOf(jsonList.get(i));
            JSONObject jsonObject = JSONUtil.parseObj(strTypeJson);
            editView1.setText(jsonObject.getStr("demand_order_number"));
            editView1.setInputType(InputType.TYPE_NULL);
            editText2.setText(jsonObject.getStr("waybill_id"));
            editText3.setText(jsonObject.getStr("type_of_transport"));
            editText4.setText(jsonObject.getStr("mode_of_transport"));
            editText5.setText(jsonObject.getStr("dangerous_goods_labe"));
        }
    }


    public void saveButtonOnCLick(View view) {
        EditText editView1 = findViewById(R.id.demand_order_numberEditView);
        EditText editText2 = findViewById(R.id.waybill_idEditView);
        EditText editText3 = findViewById(R.id.type_of_transportEditView);
        EditText editText4 = findViewById(R.id.mode_of_transportEditView);
        EditText editText5 = findViewById(R.id.dangerous_goods_labeEditView);

        Python py = Python.getInstance();
        PyObject obj = py.getModule("wbc").get("insertData").call(editView1.getText().toString(), editText2.getText().toString(), editText3.getText().toString(), editText4.getText().toString(), editText5.getText().toString());
        Intent intent = new Intent(this, TableActivity.class);
        this.startActivity(intent);
    }

    // 初始化Python环境
    void initPython() {
        if (!Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }
    }


    List<PyObject> callPythonCode(String demand_order_number) {
        Python py = Python.getInstance();
        PyObject obj = py.getModule("wbc").get("getData").call(demand_order_number);
        List<PyObject> jsonList = obj.asList();
        return jsonList;

    }

}